Skip to content

Raise on asyncio.wait and as_completed in workflows - #2155

Open
prateek11rai wants to merge 2 commits into
temporalio:mainfrom
prateek11rai:restrict-asyncio-wait-as-completed
Open

prateek11rai wants to merge 2 commits into
temporalio:mainfrom
prateek11rai:restrict-asyncio-wait-as-completed

Conversation

@prateek11rai

@prateek11rai prateek11rai commented Nov 21, 2026 •

Copy link
Copy Markdown

Note

🤖 claude-opus-5-5 on behalf of Prateek

What was changed

Calling asyncio.as_completed() or asyncio.wait() inside a workflow now raises RestrictedWorkflowAccessError. Before, the sandbox only printed a warning. The error message tells you what to use instead: workflow.as_completed() or workflow.wait().

Files:

  • temporalio/worker/workflow_sandbox/_restrictions.py: removed leaf_warning=UserWarning from the two matchers, so they raise like other restricted calls.
  • tests/worker/workflow_sandbox/test_runner.py: updated test_workflow_sandbox_bad_asyncio to expect the error instead of the warnings.
  • README.md: the GreetingWorkflow example now uses workflow.wait (it would fail otherwise; same fix as Use workflow.wait in README GreetingWorkflow example #2091), and the asyncio section says the sandbox raises.
  • changelog/breaking-changes/impatient-hourglass-heron.md: release note.

Why?

Both functions put tasks in a set. A set's order depends on memory addresses, which differ between workers, so a workflow can issue its commands in a different order on replay and fail with a nondeterminism error.

The warning was added in 1.7.0 (August 2024) as a first step. That release's notes said: "In a future release it will be an error to use non-deterministic versions from asyncio." This PR makes that change, as planned in #535.

What users will notice:

  • Workflow code that still calls these functions now fails its workflow task, which keeps retrying until the code is fixed. The workflow itself does not fail.
  • Switching a running workflow from asyncio.wait to workflow.wait can change the order of its commands, so the release note recommends doing it with versioning (patching).
  • asyncio.TaskGroup stays a warning. Its warning only shipped in 1.36.0 (Warn against asyncio TaskGroup in workflows #2085), and there is no deterministic replacement yet.

Checklist

  1. Closes [Feature Request] Turn invalid asyncio call warnings into errors #535

  2. How was this tested:

    The updated test runs twice, once for as_completed and once for wait. Each run checks that the workflow fails with RestrictedWorkflowAccessError and that the message names the workflow. replacement.

    Code Test Result
    main updated test fails: DID NOT RAISE WorkflowFailureError
    this PR updated test passes (2 of 2)
    this PR old test hangs until the 60 s timeout, because the workflow task keeps retrying
    this PR all of tests/worker/workflow_sandbox 56 passed, 1 skipped (55 passed on main; the old test is now two)

    I also ran the full suite on main and on this branch and compared the results: no test fails only on this branch. Both runs had unrelated failures, mostly Nexus tests, because I used Homebrew's temporal dev server (1.9.1) instead of the build the tests download. poe lint passes.

    Run on macOS arm64, Python 3.13.3, with -E localhost:7233.

  3. Any docs updates needed?

    README updated in this PR, plus the changelog fragment.

prateek11rai and others added 2 commits November 24, 2026 15:06
The sandbox has warned on these set-based calls since 1.7.0; they now
raise RestrictedWorkflowAccessError like other non-deterministic calls.
Update the sandbox test and the README example to match.

Closes temporalio#535

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alex Mazzeo <alex.mazzeo@outlook.com>
@VegetarianOrc
VegetarianOrc force-pushed the restrict-asyncio-wait-as-completed branch from 6186099 to 181992d Compare November 24, 2026 23:06

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Turn invalid asyncio call warnings into errors

2 participants